package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bean.Good;
import com.bean.GoodMessage;
import com.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName : GoodController
 * @Auther : acerolan@foxmail.com
 * @Data : 2024/9/10 19:00
 * @Description :
 */
@RestController
public class GoodController {

    @Autowired
    GoodService goodService;

    @GetMapping("/good/getGoods")
    public List<Map<String, Object>> getGoodsByUserId(@RequestParam("userId") int userId) {
        List<Map<String, Object>> goodsList = goodService.getGoodsByUserId(userId);

        return goodsList;
    }

    @GetMapping("/good/getGoodInfo")
    public Map<String, Object> getGoodInfoByGoodId(@RequestParam("goodId") int goodId) {
        Map<String, Object> goodInfo = goodService.getGoodInfoByGoodId(goodId);

        return goodInfo;
    }

    @PostMapping("/good/sendGoodMessage")
    public Map<String, Object> recvGoodMessage(@RequestBody Map<String, Object> goodMessage) {
//        System.out.println(goodMessage);
        int goodId = (int) goodMessage.get("goodId");
        Object message = goodMessage.get("message");

        // 转map
        String messageJsonStr = JSON.toJSONString(message);
        Map<String, Object> messageMap = JSON.parseObject(messageJsonStr);
//        System.out.println(messageMap);

        GoodMessage msg = new GoodMessage();
        msg.setGoodId(goodId);

        int no = goodService.countGoodMessagesByGoodId(goodId) + 1;
        msg.setNo(no);

        // HashMap<String,Object> 类型存入一个实体,取出实体报错 类型转换异常
        // 用 Integer fatherNo = (Integer) messageMap.get("fatherNo") 会报错
//        Integer fatherNo = new ObjectMapper().convertValue(messageMap.get("fatherNo"), Integer.TYPE);
//        System.out.println(fatherNo);

        int fatherNo = Integer.parseInt(messageMap.get("fatherNo") + "");
        msg.setFatherNo(fatherNo);

        int userId = Integer.parseInt(messageMap.get("userId") + "");
        msg.setUserId(userId);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String formattedDate = formatter.format(date);
        msg.setDate(formattedDate);

        String text = (String) messageMap.get("text");
        msg.setText(text);

        String sendType = (String) messageMap.get("sendType");
        msg.setSendType(sendType);

        int sendToUserId = Integer.parseInt(messageMap.get("sendToUserId") + "");
        msg.setSendToUserId(sendToUserId);

        String sendToUserNickName = (String) messageMap.get("sendToUserNickName");
        msg.setSendToUserNickName(sendToUserNickName);

        String messageChecked = (String) messageMap.get("messageChecked");
        if ("true".equals(messageChecked)) {
            msg.setMessageChecked(true);
        } else {
            msg.setMessageChecked(false);
        }

        int rt = goodService.addGoodMessage(msg);
        System.out.println(rt);

        goodMessage.put("no", no);

        return goodMessage;
    }
    @GetMapping("/good/seleall")
    public List<Map<String, Object>> seletgoods(@RequestParam("page") int page) {
        List<Map<String, Object>> seletgoods =  goodService.seletgoods(page);
        System.out.println("查询页"+page);
        return seletgoods;
    }
    @GetMapping("/good/Search")
    public List<Map<String, Object>> Search(@RequestParam("page") int page,@RequestParam("text") String text) {
//        List<Map<String, Object>> seletgoods =  goodService.seletgoods(page);
        List<Map<String, Object>> search = goodService.Search(page, text);
        System.out.println("页数"+page+"文字"+text);
        return search;
    }

    @PostMapping("/good/insertGood")
    public Map<String, Integer> uploadGood(@RequestBody Map<String, Object> goodMap) {
        Object goodObj = goodMap.get("good");
        // 转map
        String goodStr = JSON.toJSONString(goodObj);
        Map<String, Object> good = JSON.parseObject(goodStr);
        System.out.println(good);
        Good goodBean = new Good();
        goodBean.setAddress(good.get("address") + "");
        goodBean.setDescription(good.get("description") + "");
        goodBean.setMode1((Boolean) good.get("mode1"));
        goodBean.setMode2((Boolean) good.get("mode2"));
        goodBean.setMode3((Boolean) good.get("mode3"));
        goodBean.setPrice(Double.parseDouble(good.get("price") + ""));
        goodBean.setUserId(Integer.parseInt(good.get("user_id") + ""));
        goodBean.setStatus(good.get("status") + "");
        goodBean.setSoldOut((Boolean) good.get("sold_out"));

        goodService.insertGood(goodBean);

        List<Good> goodsList = goodService.getGoodsOnlyInfoByUserId(goodBean.getUserId());
        int goodId = goodsList.get(goodsList.size() - 1).getId();

        Map<String, Integer> map = new HashMap<>();
        map.put("goodId", goodId);

        return map;
    }

    /**
     * 此方法用于私信界面获取对方信息
     *
     * @param inputMap
     * @return outputData
     */
    @PostMapping("/good/lianXiTa")
    public Map<String, Object> lianXiTa(@RequestBody Map<String, Object> inputMap) {
        int tid = Integer.parseInt(inputMap.get("Tid") + "");
        int gid = Integer.parseInt(inputMap.get("Gid") + "");

        Map<String, Object> data = goodService.lianXiTa(tid, gid);

        Map<String, Object> outputData = new HashMap<>();
        if (data != null) {
            outputData.put("code", 200);
            outputData.put("msg", "获取对方信息于商品信息成功！");
            outputData.put("data", data);
        } else {
            outputData.put("code", 404);
            outputData.put("msg", "获取对方信息于商品信息失败！");
            outputData.put("data", null);
        }

        return outputData;
    }


}
